package com.spark.client.builder;

import com.spark.client.SparkClient;
import com.spark.client.config.SparkConfig;
import com.spark.client.model.SparkMessage;
import com.spark.client.tool.SparkTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 星火大模型构建器
 * 支持链式调用配置各种参数
 */
public class SparkBuilder {
    
    private final SparkConfig config;
    private String model;
    private String user;
    private List<SparkMessage> messages;
    private Double temperature;
    private Double topP;
    private Integer topK;
    private Double presencePenalty;
    private Double frequencyPenalty;
    private Boolean stream;
    private Integer maxTokens;
    private List<SparkTool> tools;
    private Map<String, Object> responseFormat;
    private Object toolChoice;

    public SparkBuilder(SparkConfig config) {
        this.config = config;
        this.messages = new ArrayList<>();
        this.tools = new ArrayList<>();
        // 设置默认值
        this.model = config.getModel();
        this.maxTokens = config.getMaxTokens();
        this.temperature = config.getTemperature();
        this.stream = false;
    }

    /**
     * 设置模型版本
     * @param model 模型版本 (4.0Ultra, generalv3.5, max-32k, generalv3, pro-128k, lite)
     * @return SparkBuilder
     */
    public SparkBuilder model(String model) {
        this.model = model;
        return this;
    }

    /**
     * 设置用户ID
     * @param user 用户唯一标识
     * @return SparkBuilder
     */
    public SparkBuilder user(String user) {
        this.user = user;
        return this;
    }

    /**
     * 添加用户消息
     * @param content 消息内容
     * @return SparkBuilder
     */
    public SparkBuilder userMessage(String content) {
        this.messages.add(SparkMessage.user(content));
        return this;
    }

    /**
     * 添加系统消息
     * @param content 系统消息内容
     * @return SparkBuilder
     */
    public SparkBuilder systemMessage(String content) {
        this.messages.add(SparkMessage.system(content));
        return this;
    }

    /**
     * 添加助手消息
     * @param content 助手消息内容
     * @return SparkBuilder
     */
    public SparkBuilder assistantMessage(String content) {
        this.messages.add(SparkMessage.assistant(content));
        return this;
    }

    /**
     * 添加消息
     * @param message 消息对象
     * @return SparkBuilder
     */
    public SparkBuilder message(SparkMessage message) {
        this.messages.add(message);
        return this;
    }

    /**
     * 添加多条消息
     * @param messages 消息列表
     * @return SparkBuilder
     */
    public SparkBuilder messages(List<SparkMessage> messages) {
        this.messages.addAll(messages);
        return this;
    }

    /**
     * 设置温度参数
     * @param temperature 温度值 (0-2)
     * @return SparkBuilder
     */
    public SparkBuilder temperature(Double temperature) {
        this.temperature = temperature;
        return this;
    }

    /**
     * 设置核采样阈值
     * @param topP 核采样阈值 (0-1]
     * @return SparkBuilder
     */
    public SparkBuilder topP(Double topP) {
        this.topP = topP;
        return this;
    }

    /**
     * 设置随机选择参数
     * @param topK 随机选择参数 [1-6]
     * @return SparkBuilder
     */
    public SparkBuilder topK(Integer topK) {
        this.topK = topK;
        return this;
    }

    /**
     * 设置重复词惩罚值
     * @param presencePenalty 重复词惩罚值 [0-2]
     * @return SparkBuilder
     */
    public SparkBuilder presencePenalty(Double presencePenalty) {
        this.presencePenalty = presencePenalty;
        return this;
    }

    /**
     * 设置频率惩罚值
     * @param frequencyPenalty 频率惩罚值 [0-1]
     * @return SparkBuilder
     */
    public SparkBuilder frequencyPenalty(Double frequencyPenalty) {
        this.frequencyPenalty = frequencyPenalty;
        return this;
    }

    /**
     * 启用流式输出
     * @return SparkBuilder
     */
    public SparkBuilder stream() {
        this.stream = true;
        return this;
    }

    /**
     * 设置是否流式输出
     * @param stream 是否流式输出
     * @return SparkBuilder
     */
    public SparkBuilder stream(Boolean stream) {
        this.stream = stream;
        return this;
    }

    /**
     * 设置最大 tokens
     * @param maxTokens 最大 tokens 数量
     * @return SparkBuilder
     */
    public SparkBuilder maxTokens(Integer maxTokens) {
        this.maxTokens = maxTokens;
        return this;
    }

    /**
     * 添加工具
     * @param tool 工具对象
     * @return SparkBuilder
     */
    public SparkBuilder tool(SparkTool tool) {
        this.tools.add(tool);
        return this;
    }

    /**
     * 添加多个工具
     * @param tools 工具列表
     * @return SparkBuilder
     */
    public SparkBuilder tools(List<SparkTool> tools) {
        this.tools.addAll(tools);
        return this;
    }

    /**
     * 启用联网搜索
     * @return SparkBuilder
     */
    public SparkBuilder enableWebSearch() {
        return enableWebSearch(true, false, "normal");
    }

    /**
     * 配置联网搜索
     * @param enable 是否启用
     * @param showRefLabel 是否显示引用标签
     * @param searchMode 搜索模式 (normal/deep)
     * @return SparkBuilder
     */
    public SparkBuilder enableWebSearch(boolean enable, boolean showRefLabel, String searchMode) {
        Map<String, Object> webSearchTool = new HashMap<>();
        webSearchTool.put("type", "web_search");
        
        Map<String, Object> webSearchConfig = new HashMap<>();
        webSearchConfig.put("enable", enable);
        webSearchConfig.put("show_ref_label", showRefLabel);
        webSearchConfig.put("search_mode", searchMode);
        
        webSearchTool.put("web_search", webSearchConfig);
        
        // 这里需要转换为 SparkTool 格式，暂时使用 Map
        return this;
    }

    /**
     * 设置响应格式为 JSON
     * @return SparkBuilder
     */
    public SparkBuilder jsonResponse() {
        this.responseFormat = new HashMap<>();
        this.responseFormat.put("type", "json_object");
        return this;
    }

    /**
     * 设置工具选择策略
     * @param toolChoice 工具选择策略 (auto/none/required 或具体函数)
     * @return SparkBuilder
     */
    public SparkBuilder toolChoice(Object toolChoice) {
        this.toolChoice = toolChoice;
        return this;
    }

    /**
     * 构建 SparkClient
     * @return SparkClient 实例
     */
    public SparkClient build() {
        return new SparkClient(this);
    }

    // Getters
    public SparkConfig getConfig() {
        return config;
    }

    public String getModel() {
        return model;
    }

    public String getUser() {
        return user;
    }

    public List<SparkMessage> getMessages() {
        return messages;
    }

    public Double getTemperature() {
        return temperature;
    }

    public Double getTopP() {
        return topP;
    }

    public Integer getTopK() {
        return topK;
    }

    public Double getPresencePenalty() {
        return presencePenalty;
    }

    public Double getFrequencyPenalty() {
        return frequencyPenalty;
    }

    public Boolean getStream() {
        return stream;
    }

    public Integer getMaxTokens() {
        return maxTokens;
    }

    public List<SparkTool> getTools() {
        return tools;
    }

    public Map<String, Object> getResponseFormat() {
        return responseFormat;
    }

    public Object getToolChoice() {
        return toolChoice;
    }
}
